package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.factory.Factory;

/**
 * 学生业务处理类
 */
public class StudentService {
    //创建Dao对象
    private BaseStudentDao dao = Factory.getStudentDao();

    public boolean addStudent(Student student) {


        //调用dao方法
        return dao.addStudent(student);

    }

    //判断学号是否灿在
    public boolean isExists(String sId) {
        boolean result = false;
        //dao.isExists(sId);这个时  Dao不干
        Student[] stus = dao.findAllStudents();
        //判断学号是否存在
        for (int i = 0; i < stus.length; i++) {

            //stus[i]
            Student student = stus[i];
            if (student != null && student.getSid().equals(sId)) {
                result = true;
                break;
            }

        }
        return result;
    }

    public Student[] findAllStudents() {
        Student[] allStudents = dao.findAllStudents();
        //标记思想
        boolean flag=false;
        for (int i = 0; i < allStudents.length; i++) {
            //allStudents[i]
            if(allStudents[i]!=null){
                flag=true;
                break;
            }
        }
        if(flag){
            return allStudents;
        }else{
            return  null;
        }

    }

    public void deleteStudent(String deleteId) {
        dao.deleteStudent(deleteId);


    }

    public void updateStudent(String updateId, Student student) {
        dao.updateStudent(updateId,student);
    }
    //获取学号所在的索引
    public int getIndex(String sid) {

        var result = -1;
        //dao.isExists(sId);这个时  Dao不干
        Student[] stus = dao.findAllStudents();
        //判断学号是否存在
        for (int i = 0; i < stus.length; i++) {

            //stus[i]
            Student student = stus[i];
            if (student != null && student.getSid().equals(sid)) {
                result =i ;
                break;
            }

        }
        return result;
    }
    //通过索引删除指定的学生对象
    public void deleteStudentByIndex(int index) {

        dao.deleteStudentByIndex(index);
    }



    // //用在service 查找索引的方式 来删除

}
